const BASE_URL = 'https://tea.qingnian8.com/api/bizhi'
const TIMEOUT = 3000

const request = (options) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + options.url,
			timeout: TIMEOUT,
			method: options.method,
			data: options.data || {},
			header: {
				'access-key': 'openapi_zouh'
			},
			success(res) {
				const code = res.data.errCode
				if (code === 0) {
					return resolve(res.data)
				}
				if (code === 400) {
					uni.showModal({
						title: '错误提示',
						content: res.data.errMsg,
						showCancel: false
					})
				} else {
					uni.showToast({
						title: res.data.errMsg,
						icon: 'none'
					})
				}
				reject(res.data)
			},
			fail(err) {
				uni.showToast({
					title: res.data.errMsg,
					icon: 'none'
				})
				reject(err)
			}
		})
	})
}

export default request
